<template>
  <div>
    正在授权中....
  </div>
</template>

<script>
  import {GetQueryString} from '@/api/getQueryString';

  export default {
    name:'auth',
    data() {
      return {
        userInfo: '',
      };
    },
    computed: {

    },
    created() {
      let code  = GetQueryString('code')
      // 查看是否包含code

        if(code){
          // 如果有用code请求用户信息
          this.$axios.post(this.urlList.authUrl + '/api/wechat/get_user_info',{code:code}).then((res)=>{
            window.localStorage.setItem("userInfo", JSON.stringify(res.data.info))
            if(res.data.info.openid){
              this.$axios.post(this.urlList.authUrl + '/api/game/get_token',{openid:res.data.info.openid}).then((tokenInfo)=>{
                if(tokenInfo.data.code == 200){
                  window.localStorage.setItem("token",tokenInfo.data.token)
                  this.$message({
                    message:'授权成功',
                    type:'success'
                  })
                  let roomId = window.localStorage.getItem('room_id')
                  let pwd = window.localStorage.getItem('pwd')
                  //跳转
                  if(roomId && pwd){

                    location.href = 'http://view.lezhier.com/#/search'
                    return false
                  }
                  if(roomId){
                    this.$router.replace({name:'search'});
                  }else{
                    this.$router.replace({name:'index'});
                  }
                }else{
                  this.$message({
                    message:tokenInfo.data.message,
                    type:'error'
                  })
                }
              })
            }
          })
        }else{
          let roomId = GetQueryString("room_id");
          let pwd = GetQueryString("pwd");
          window.localStorage.setItem('room_id',roomId)
          window.localStorage.setItem('pwd',pwd)
          // 如果没有code就得去授权
          this.ReturnGetCodeUrl();
        }
      },
    methods: {

      async ReturnGetCodeUrl() {
        let redirect_uri = encodeURIComponent(this.urlList.webUrl);
        // location.href = this.config.authUrl + '/wechat/get_user_code?password=123698745'+'&redirect_uri=' + redirect_uri;
        location.href = this.urlList.authUrl + '/Api/wechat/get_user_code?password=123698745'+'&redirect_uri=' + redirect_uri;
      },


    },
    watch: {},

    components: {},


    mounted: function () {
    }
  }
</script>
<style scoped>

</style>
